package com.telecomyt.jwportal.activity.register;

import com.telecomyt.jwportal.activity.register.bean.RegisterBean;
import com.telecomyt.jwportal.base.BasePresenter;
import com.telecomyt.jwportal.http.RetrofitManager;
import com.telecomyt.jwportal.utils.RxUtils;
import com.telecomyt.jwportal.utils.ToastUtils;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @author created by chengDa
 * @time 2020/8/25  16:15
 * @description
 */
public class RegisterPresenter extends BasePresenter<RegisterContract.Model, RegisterContract.View> implements RegisterContract.Persenter {
    public RegisterPresenter(LifecycleProvider<ActivityEvent> provider) {
        super(provider);
    }

    @Override
    public void toRegister(String username, String password, String repassword) {
        getView().showLoading();
        RetrofitManager.getInstance()
                .getDefaultReq()
                .register(username, password, repassword)
                .compose(getProvider().<RegisterBean>bindUntilEvent(ActivityEvent.DESTROY))
                .compose(RxUtils.setThread())
                .subscribe(new Observer<RegisterBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(RegisterBean registerBean) {
                        if (registerBean != null) {
                            if (registerBean.getErrorCode() == 0) {
                                getView().regSuccess(registerBean);
                            } else {
                                ToastUtils.showShort(registerBean.getErrorMsg());
                            }
                        }
                        getView().dismissLoading();

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().dismissLoading();

                    }

                    @Override
                    public void onComplete() {
                        getView().dismissLoading();
                    }
                });
    }


    @Override
    protected RegisterContract.Model creatModule() {
        return null;
    }


}
